package cn.bocweb.spinning.view.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ErrorBean;
import cn.bocweb.spinning.model.Bean.ProductCollectBean;
import cn.bocweb.spinning.model.Bean.ProductDetailBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.common.BaseActivity;
import de.greenrobot.event.EventBus;

public class ProductDetailActivity extends BaseActivity implements View.OnClickListener {

    boolean isCollect;
    UserPresenter presenter;
    String id, userid;

    @Bind(R.id.product_detail_collect)
    ImageView productDetailCollect;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.product_detail_web)
    WebView productDetailWeb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        presenter = new UserPresenterImp();

        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(this);
        userid = dataEntity.getUserid();

        initToolbar();
        initIntent();
        initClickBtn();
        initWeb();

        showLoading();
        presenter.getProductDetail(id);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(ProductDetailBean bean) {
        hideLoading();
        if (bean.getReturn_code() == 0) {
            isCollect = Integer.parseInt(bean.getCollect()) == 1 ? true : false;
            int collectIcon = isCollect ? R.mipmap.collected : R.mipmap.product_detail_collect;
            productDetailCollect.setImageResource(collectIcon);
            productDetailWeb.loadUrl(bean.getUrl());
        } else {
            DeviceUtil.MyToast(this, bean.getMsg());
        }
    }

    public void onEventMainThread(ProductCollectBean bean) {
        if (bean.getReturn_code() == 0) {
            isCollect = !isCollect;
            int collectIcon = isCollect ? R.mipmap.collected : R.mipmap.product_detail_collect;
            productDetailCollect.setImageResource(collectIcon);
        }

    }

    public void onEventMainThread(ErrorBean bean) {
        DeviceUtil.MyToast(this, bean.getErrorMsg());
    }

    private void initIntent() {
        id = getIntent().getStringExtra(Constant.TO_PRODUCT_DETAIL_ID);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.product_detail_collect: {
                String type = isCollect ? "2" : "1";
                presenter.collectProduct(id, userid, type);
                break;
            }
        }
    }

    private void initClickBtn() {
        productDetailCollect.setOnClickListener(this);
    }

    private void initWeb() {

    }

    private void initToolbar() {
        toolbar.setTitle("产品详情");
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


}
